<?php

namespace Boer\Controller;
use Common\Controller\HomebaseController;
/**
 * 前端
 */
class FrontController extends HomebaseController {
    protected $page_num = 4;
    protected $m_Boer_banner,$m_Article,$m_Article_type,$m_Contact,$m_Consult,$m_Project;
    protected $article_type_list = array(
        //关于我们
        'aboutus'=>array(
            '1'=>array(2,3,4,5),
            '2'=>array(13,14,15,16),
        ),
        //解决方案
        'solution'=>array(
            '1'=>array(10),//中文
            '2'=>array(21)
        ),
        //投资运营
        'operate'=>array(
            '1'=>array(11),  //中文
            '2'=>array(22),    //英文
        ),
        //新闻中心
        'news'=>array(
            '1'=>array(7,8,9),  //中文
            '2'=>array(18,19,20),    //英文
        ),
        //洽谈合作
        'cooperate'=>array(
            '1'=>array(25,27,28),
            '2'=>array(29,31,32),
        )
    );
    //想合作的类型
    protected  $cooperate_type = array(
            '1'=>array(
                '1'=>'供应商',
                '2'=>'分销商',
                '3'=>'区域合作伙伴',
                '4'=>'技术合作伙伴',
                '5'=>'投资',
                '6'=>'企业主'
            ),
            '2'=>array(
                '1'=>'supplier',
                '2'=>'Distributor',
                '3'=>'Regional partners',
                '4'=>'Technical partner',
                '5'=>'Investment',
                '6'=>'Business owners'
            )
    );
    //公司类型
    protected  $company_type = array(
        '1'=>array(
            '1'=>'国有企业',
            '2'=>'民营企业',
            '3'=>'上市公司',
            '4'=>'外资企业',
            '5'=>'其他',
        ),
        '2'=>array(
            '1'=>'State - owned enterprises',
            '2'=>'Private Enterprise',
            '3'=>'Listed company',
            '4'=>'Foreign companies',
            '5'=>'others',
        )
    );
    //项目类型
    protected $project_type = array(
        '1'=>array(
            '1'=>'分布式光伏发电',
            '2'=>'大型电力工程',
            '3'=>'微电网及储能工程'
        ),
        '2'=>array(
            '1'=>'Distributed Solar Generation',
            '2'=>'Large Electrical Power engineering',
            '3'=>'Microgrid and energy storage engineering'
        )
    );
    protected $project_1 = array(58,55);
    protected $project_2 = array(59,56);
    protected $project_3 = array(60,57);


    function _initialize() {
        parent::_initialize();
        $this->m_Boer_banner= D("Boer/Boer_banner");
        $this->m_Article= D("Boer/Article");
        $this->m_Article_type= D("Boer/Article_type");
        $this->m_Contact= D("Boer/Contact");
        $this->m_Consult= D("Boer/Boer_consult");
        $this->m_Project= D("Boer/Project");
        //获取底部菜单
        //解决方案最新三条
        $bottom_solution_list = $this->get_article("solution",'',0,4,"id");
        $this->assign("bottom_solution_list",$bottom_solution_list);

        $bottom_operate_list = $this->get_article("operate",'',0,4,"id");
        $this->assign("bottom_operate_list",$bottom_operate_list);
        //获取联系方式
        $contact = $this->m_Contact->where(array("language"=>$this->language))->find();
        $this->assign("contact",$contact);
        $this->assign("language",$this->language);
        $keyword = I("post.keyword");
        $this->assign("keyword",$keyword);
    }

    //文章获取
    public function get_article($type,$n_where,$limit_from = "",$limit_to = "",$order = "update_time desc"){
        $tid_list = $this->article_type_list[$type][$this->language];
        if(empty($tid_list)){
            die("文章数据获取失败!");
        }
        $where = array("tid"=>array("IN",$tid_list));
        if(!empty($n_where)){
            $where = array_merge($where,$n_where);
        }
        if(!empty($limit_to)){
            if(empty($limit_from)){
                $limit_from  = 0;
            }
            $list = $this->m_Article->where($where)
                ->order($order)
                ->limit($limit_from, $limit_to)
                ->select();
        }else{
            $list = $this->m_Article->where($where)
                ->order($order)
                ->select();
        }

        return $list;
    }

    /**
     * 获取文章详情页
     */
    public function article_detail(){
        $id = I("get.id");
        $tid = I("get.tid");
        if(empty($id) && empty($tid)){
            $this->error("参数有误！");
        }
        if(!empty($tid) && empty($id)){
            $this->m_Article->where(array("tid"=>$tid))->save(array("read_num"=>"read_num+1"));
            $article = $this->m_Article->where(array("tid"=>$tid))->find();
            $id = $article['id'];
        }else{
            $article = $this->m_Article->where(array("id"=>$id))->find();
        }

        $this->m_Article->where(array("id"=>$id))->save(array("read_num"=>$article['read_num']+1));
        $article = $this->m_Article->where(array("id"=>$id))->find();
        if(!sp_is_mobile()){
            $article['content'] = $this->pageBreak(stripcslashes($article['content']));
        }else{
            $article['content'] = str_replace('_ueditor_page_break_tag_','',$article['content']);
        }

        $this->assign("article",$article);

        $article_type = $this->m_Article_type->where(array("id"=>$article['tid']))->find();

        if($article_type['f_id'] == 0){
            $t_title1 = $article_type['title'];
            $t_title2 = $article['title'];

            //获取文章列表
            $bottom_article_list = $this->m_Article->where(array("tid"=>$article['tid']))
                ->order("id")
                ->select();
            foreach($bottom_article_list AS &$bitem){
                if(in_array($id,$this->project_1)){
                    $project_list = $this->m_Project->where(array("tid"=>1,"language"=>$this->language))->select();
                    $this->assign("project_list",$project_list);
                }elseif(in_array($id,$this->project_2)){
                    $project_list = $this->m_Project->where(array("tid"=>2,"language"=>$this->language))->select();
                    $this->assign("project_list",$project_list);
                }elseif(in_array($id,$this->project_3)){
                    $project_list = $this->m_Project->where(array("tid"=>3,"language"=>$this->language))->select();
                    $this->assign("project_list",$project_list);
                }

                $bitem['url'] = U("Front/article_detail",array("id"=>$bitem['id']));
                if($bitem['id'] == $id){
                    $bitem['check'] = 1;
                }
            }
            $this->assign("top_f_id",$article_type['id']);

        }else{
            $article_type_f = $this->m_Article_type->where(array("id"=>$article_type['f_id']))->find();
            $t_title1 = $article_type_f['title'];
            $t_title2 =  $article_type['title'];

            $bottom_article_list = $this->m_Article_type->where(array("f_id"=>$article_type_f['id']))->order("id")->select();
            foreach($bottom_article_list AS &$bitem){
                if(in_array($bitem['id'],array(26,30))){
                    $bitem['url'] = U('front/consult');
                }elseif(in_array($bitem['id'],$this->article_type_list['aboutus'][$this->language]) || in_array($bitem['id'],$this->article_type_list['cooperate'][$this->language])){
                    $article_item = $this->m_Article->where(array("tid"=>$bitem['id']))->find();
                    $bitem['url'] = U("Front/article_detail",array("id"=>$article_item['id']));
                }else{
                    $bitem['url'] = U("Front/news_list",array("tid"=>$bitem['id']));
                }

                if($article['tid'] == $bitem['id']){
                    $bitem['check'] = 1;
                }
            }
            $this->assign("top_f_id",$article_type_f['id']);
        }

        $this->assign("bottom_article_list",$bottom_article_list);
        $this->assign("t_title1",$t_title1);
        $this->assign("t_title2",$t_title2);
        if(isset($project_list) && !empty($project_list) && !sp_is_mobile()){
            $this->display(":Front/project");
        }else{
            $this->display();
        }

    }

    /**
     * 新闻列表页
     */
    public function news_list(){
        $tid = I("get.tid");
        $page  = I('get.page');
        if(empty($page)){
            $page = 1;
        }
        if(empty($tid)){
            $this->error("参数有误！");
        }
        $article_type = $this->m_Article_type->where(array("id"=>$tid))->find();
        $this->assign("article_type",$article_type);
        $article_type_fater = $this->m_Article_type->where(array("id"=>$article_type['f_id']))->find();
        $this->assign("article_type_fater",$article_type_fater);

        $list = $this->get_article('news',array("tid"=>$tid),($page-1)*$this->page_num,$this->page_num);
        $where = array("tid"=>$tid);
        $count = $this->m_Article->where($where)->count();
        $this->assign("page_num",ceil($count/$this->page_num));
        $this->assign("page",$page);
        $this->assign("count",$count);
        $this->assign("list",$list);
        $this->assign("url",U('front/news_list',array('tid'=>$tid)));
        //获取底部菜单
        $type_list = $this->m_Article_type->where(array("f_id"=>$article_type['f_id']))->order("id")->select();
        foreach($type_list AS &$titem){
            $titem['url'] =  U("Front/news_list",array("tid"=>$titem['id']));
            if($titem['id'] == $tid){
                $titem['check'] = 1;
            }
        }

        $this->assign("top_f_id",$article_type_fater['id']);
        $this->assign("type_list",$type_list);
        $this->display();
    }

    /**
     * 引导页
     */
    public function guide(){
       // if(!sp_is_mobile()) {
            $this->display();
//        }else{
//            header("location:".U("front/index"));
//        }

    }

    //首页
    public function index() {
        $where = array("language"=>$this->language,'type'=>1);
        //首页banner图
        $banner_list = $this->m_Boer_banner->where($where)->select();
        $this->assign("banner_list",$banner_list);
        //解决方案，顶部6个
        $where = array("language"=>$this->language,'type'=>2);
        $solution_list = $this->m_Boer_banner->where($where)->select();
        $this->assign("solution_list",$solution_list);
        //投资运营
//        $operate_list = $this->get_article("operate",array("is_home"=>1),0,3);
//        $this->assign("operate_list",$operate_list);

        //最新的5条新闻
        if($this->language == 1){
            $home_news_list1 = $this->get_article("news",array('tid'=>9),0,12);
            $home_news_list2 = $this->get_article("news",array('tid'=>7),0,12);
            $home_news_list3 = $this->get_article("news",array('tid'=>8),0,12);
        }else{
            $home_news_list1 = $this->get_article("news",array('tid'=>20),0,12);
            $home_news_list2 = $this->get_article("news",array('tid'=>18),0,12);
            $home_news_list3 = $this->get_article("news",array('tid'=>19),0,12);
        }
        $this->assign("home_news_list1",$home_news_list1);
        $this->assign("home_news_list2",$home_news_list2);
        $this->assign("home_news_list3",$home_news_list3);
        $new_5_list = $this->get_article("news",'',0,4);
        $this->assign("new_5_list",$new_5_list);
        $this->display();
    }

    /**
     * 项目
     */
    public function project(){
        $tid = I("get.tid");
        if(empty($tid)){
            die("参数有误!");
        }
        $list = $this->m_Project->where(array("tid"=>$tid))->select();
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 洽谈合作
     */
    public function cooperate(){
        $list = $this->get_article("cooperate","",0,1);
        $article = $list[0];
        $this->assign("article",$article);
        $this->assign("top_f_id",$article['tid']);
        $this->display();
    }


    /**
     * 在线咨询
     */
    public function consult(){
        if($this->language == 1){
            $tid = 26;
            $f_id = 23;
            $t_title2 = "在线咨询";
        }else{
            $f_id = 24;
            $tid = 30;
            $t_title2 = "Online consultation";
        }
        $article_type_f = $this->m_Article_type->where(array("id" =>$f_id))->find();
        $t_title1 = $article_type_f['title'];

        $bottom_article_list = $this->m_Article_type->where(array("f_id" => $article_type_f['id']))->order("id")->select();
        foreach ($bottom_article_list AS &$bitem) {
            if(in_array($bitem['id'],array(26,30))){
                $bitem['url'] = U('front/consult');
            }elseif(in_array($bitem['id'],$this->article_type_list['aboutus'][$this->language]) || in_array($bitem['id'],$this->article_type_list['cooperate'][$this->language])){
                $article_item = $this->m_Article->where(array("tid"=>$bitem['id']))->find();
                $bitem['url'] = U("Front/article_detail",array("id"=>$article_item['id']));
            }

            if($tid == $bitem['id']){
                $bitem['check'] = 1;
            }
        }

        $this->assign("bottom_article_list",$bottom_article_list);
        $this->assign("t_title1",$t_title1);
        $this->assign("t_title2",$t_title2);

        $this->assign("cooperate_type",$this->cooperate_type[$this->language]);
        $this->assign("company_type",$this->company_type[$this->language]);
        $this->display();
    }

    /**
     * 在先咨询提交
     */
    public function consult_post(){
        $insert = I("post.");
        $insert['create_time'] = time();
        $insert['language'] = $this->language;
        $result = $this->m_Consult->add($insert);
        if($result){
            echo "<script>alert('提交成功');location.href='/';</script>";
        }else{
            echo "<script>alert('提交失败');history.go(-1);</script>";
        }
    }

    /**
     * 搜索
     */
    public function search(){
        $keyword = I("post.keyword");
        $where = array();
        $where['title'] = array('like',"%$keyword%");
        $where['language'] = $this->language;
        $list = $this->m_Article->where($where)
            ->order("update_time desc")
            ->select();
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 切换语言
     */
    public function change_language(){
        $language = I("get.language");
        setcookie("language",$language);
        header("Location:".U("front/index"));
    }

    function pageBreak($content){
        $strSplit = preg_split('/_ueditor_page_break_tag_/', $content);
        //var_dump($strSplit);exit;
        $count    = count($strSplit);
        $outStr   = "";
        $i        = 1;

        if ($count > 1 ) {
            $outStr   = "<div id='page_break'>";
            foreach($strSplit as $value) {
                if ($i <= 1) {
                    $outStr .= "<div id='page_$i'>$value</div>";
                } else {
                    $outStr .= "<div id='page_$i' class='collapse'>$value</div>";
                }
                $i++;
            }

            $outStr .= "<div class='num'>";
            for ($i = 1; $i <= $count; $i++) {
                $outStr .= "<li>$i</li>";
            }
            $outStr .= "</div></div>";
            return $outStr;
        } else {
            return $content;
        }
    }


}


